package com.atguigu.lease.web.admin.controller.system;

import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.common.util.JwtUtil;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.web.admin.dto.AdminLoginDto;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.AdminCaptchaVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@Tag(name = "管理员登录模块")
public class LoginController {
    @Resource
    SystemUserService systemUserService;
    @Operation(summary = "获取登录验证码")
    @GetMapping("/login/captcha")
    public Result<AdminCaptchaVo> captcha(){
        /*
            image图片 base64字符串
                base64不是压缩处理方式，图片内容交给前端时，变大了：
                    三个字节转为4个字节
            后端缓存图片文本的key
         */
        return Result.ok(systemUserService.getCaptcha());
    }

    @Operation(summary = "管理员登录")
    @PostMapping("/login")
    public Result login(@RequestBody AdminLoginDto loginDto){
        // 登录成功 返回jwt token
        return Result.ok(systemUserService.login(loginDto));
    }

    @Operation(summary = "获取登录信息")
    @GetMapping("/info")
    public Result<HashMap<String, String>> info(@RequestHeader("ACCESS_TOKEN")String token,
                                   HttpServletRequest request){
        // 登录成功 返回jwt token
        Map<String, Object> map = JwtUtil.parseToken(token, request);
        HashMap<String, String> infoMap = new HashMap<>();
        infoMap.put("name" , map.get("userName").toString());
        infoMap.put("avatarUrl" , map.get("avatar").toString());

        return Result.ok(infoMap);
    }
}
